package com.xsf.pay.common.domain;

import cn.hutool.core.date.DateUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.Serial;
import java.io.Serializable;

/**
 * @author 呆毛一哥
 */
@Data
@Slf4j
public class ResultData<T> implements Serializable {
    @Serial
    private static final long serialVersionUID = 1L;
    /**
     * 返回代码
     */
    public int code;
    /**
     * 返回对象
     */
    public T data;
    /**
     * 提示信息
     */
    private String msg;

    /**
     * 返回时间
     */
    private String timestamp;

    public ResultData() {
        this.code = CodeMsg.SUCCESS.getCode();
        this.msg = CodeMsg.SUCCESS.getMsg();
        this.timestamp = DateUtil.now();
        print();
    }

    public ResultData(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
        this.timestamp = DateUtil.now();
        print();
    }

    public ResultData(T data) {
        this.code = CodeMsg.SUCCESS.getCode();
        this.msg = CodeMsg.SUCCESS.getMsg();
        this.timestamp = DateUtil.now();
        this.data = data;
        print();
    }

    public ResultData(CodeMsg codeMsg) {
        if (codeMsg != null) {
            this.code = codeMsg.getCode();
            this.msg = codeMsg.getMsg();
            this.timestamp = DateUtil.now();
        }
        print();
    }

    public ResultData(CodeMsg codeMsg, T data) {
        if (codeMsg != null) {
            this.code = codeMsg.getCode();
            this.msg = codeMsg.getMsg();
            this.timestamp = DateUtil.now();
        }
        this.data = data;
        print();
    }

    public static <T> ResultData<T> success() {
        return new ResultData<T>();
    }

    public static <T> ResultData<T> success(T data) {
        return new ResultData<T>(data);
    }

    public static <T> ResultData<T> success(CodeMsg codeMsg, T data) {
        return new ResultData<T>(codeMsg, data);
    }

    public static <T> ResultData<T> fail() {
        return new ResultData<T>(CodeMsg.FAIL);
    }

    public static <T> ResultData<T> fail(String msg) {
        return new ResultData<T>(CodeMsg.FAIL.setMsg(msg));
    }

    public static <T> ResultData<T> fail(Integer code, String msg) {
        return new ResultData<T>(CodeMsg.FAIL.setMsg(msg));
    }

    public static <T> ResultData<T> fail(CodeMsg codeMsg) {
        return new ResultData<T>(codeMsg);
    }

    /**
     * 打印结果
     */
    private void print() {
        log.info("返回结果为:{}", this);
    }
}
